package com.ruitu.health_1.view;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ruitu.arad.base.BaseModel;
import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.base.base_list.BaseRefreshFragment;
import com.ruitu.health_1.dao.bean.Pay;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;

import java.util.List;

public abstract class NoCacheLoadingFragment<T extends BasePresenter, E extends BaseModel> extends BaseRefreshFragment<T, E> {

    /**
     * rootView是否初始化标志,防止回调函数在rootView为空的时候触发
     */
    private boolean hasCreateView;

    /**
     * 当前Fragment是否处于可见状态标志,防止因ViewPager的缓存机制而导致回调函数的触发
     */
    private boolean isFragmentVisible;

    /**
     * onCreateView()里返回的view,修饰为protected,所以子类继承该类时,在onCreateView里必须对该变量进行初始化
     */
    protected View rootView;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (rootView == null) {
            return;
        }
        hasCreateView = true;
        if (isVisibleToUser) {
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
            return;
        }
        if (isFragmentVisible) {
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVarible();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!hasCreateView && getUserVisibleHint()) {
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
        }
    }

    private void initVarible() {
        hasCreateView = false;
        isFragmentVisible = false;
    }

    /**
     * 当前我的fragment可见状态发生变化时会回调该方法
     * 如果当前fragment是第一次加载,等待onCreateView后才会回调该方法,其它情况回调时 跟{@link #setUserVisibleHint(boolean)}一致
     * 在该回调方法中你可以做一些加载数据操作,甚至是控件的操作,因为配合fragment的view复用机制
     *
     * @param isVisibleToUser true 不可见 -> 可见
     *                        false 可见 -> 不可见
     */
    protected void onFragmentVisibleChange(boolean isVisibleToUser) {

    }

    public abstract void setData(List<StoreOrderItemList> orderLists);

    public void requestSuc() {

    }

    public void confirmReceipt() {

    }


    protected void getList(int size, String status) {

    }

    public void setWaitPayData(Pay payData) {

    }

    protected ChangeOrder changeOrder;

    public class ChangeOrder {
        private String orderId;
        private String memberId;

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getMemberId() {
            return memberId;
        }

        public void setMemberId(String memberId) {
            this.memberId = memberId;
        }
    }
}
